package com.tss.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tss.pojo.Attendance;
import com.tss.pojo.AttendanceResult;
import com.tss.pojo.Result;
import com.tss.service.ApplyService;
import com.tss.service.AttendanceService;
import com.tss.service.CourseService;
import com.tss.service.RecordService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("attendance")
public class AttendanceController {
	
	@Autowired
	private AttendanceService attendanceService;
	
	@Autowired
	private ApplyService applyService;
	
	@Autowired
	private RecordService recordService;
	
	@Autowired
	private CourseService courseService;
	
	/**
	 * 创建签到表
	 * @param courseId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("create")
	@ResponseBody
	public String create(Long courseId,Long startTime,Long endTime){
		Attendance attendance = new Attendance();
		attendance.setCourseId(courseId);
		attendance.setSignInStartTime(new Date(startTime));
		attendance.setSignInEndTime(new Date(endTime));
		Result result = attendanceService.createAttendance(attendance);
		return result.toString();
	}
	
	/**
	 * 查看签到结果
	 * @param attendance
	 * @return
	 */
	@RequestMapping("select")
	@ResponseBody
	public String select(Attendance attendance){
		Result result = new Result();
		Attendance a = attendanceService.findAttendance(attendance);
		if(a==null){
			result.setErrorMessage("签到表不存在！");
			return result.toString();
		}
		result.addResult("attendance", a);
		result.addResult("predicted", applyService.countApply(a.getCourseId()));
		result.addResult("actual", recordService.countRecord(a.getId()));
		result.setSuccessMessage("查询成功！");
		return result.toString();
	}
	
	/**
	 * 获取签到表列表
	 * @param attendance
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public String list(Attendance attendance){
		List<Attendance> attendances = attendanceService.listAttendances(attendance);
		if(attendances==null||attendances.isEmpty()){
			return null;
		}
		List<AttendanceResult> atendanceResults = new ArrayList<AttendanceResult>();
		for(Attendance a:attendances){
			AttendanceResult ar = new AttendanceResult();
			ar.setId(a.getId());
			ar.setSignInStartTime(a.getSignInStartTime().getTime());
			ar.setSignInEndTime(a.getSignInEndTime().getTime());
			ar.setCourseName(courseService.findCourse(a.getCourseId()).getName());
			atendanceResults.add(ar);
		}
		
		return JSONArray.fromObject(atendanceResults).toString();
	}
	
	/**
	 * 删除签到 
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(Long id){
		return attendanceService.deleteAttendance(id).toString();
	}
	
}
